//
//  PraseProvince.m
//  EJLX
//
//  Created by 徐绍明 on 15/12/10.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "PraseProvince.h"

static PraseProvince *province = nil;
@implementation PraseProvince

+(PraseProvince *)manager{
    if (province) {
        return province;
    }
    province = [[PraseProvince alloc] init];
    
    [province praseXML];
    return province;
    
}


-(NSMutableDictionary *)dicts{
    if(!_dicts){
        _dicts = [NSMutableDictionary dictionary];
    }
    return _dicts;
}

-(void)praseXML{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"province_data.xml" ofType:nil];
   NSData *data = [[NSData alloc] initWithContentsOfFile:path];
   
    NSXMLParser *parse = [[NSXMLParser alloc] initWithData:data];
    parse.delegate = self;
    [parse parse];

}


-(void)parserDidStartDocument:(NSXMLParser *)parser{

    
     self.provinces = [NSMutableDictionary dictionary];
}
    
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
    
   if( [elementName isEqualToString:@"province"]) {
        _currentProvince = [attributeDict objectForKey:@"name"];
       _cities = [NSMutableDictionary dictionary];
    
 }
 else if( [elementName isEqualToString:@"city"]) {
            _currentCity = [attributeDict objectForKey:@"name"];
     
     
 }
    else if( [elementName isEqualToString:@"district"]) {
        NSString *qu  =[attributeDict objectForKey:@"name"];
        NSString *zipcode  =[attributeDict objectForKey:@"zipcode"];
        [self.dicts setObject:@{@"name":qu,@"zipcode":zipcode} forKey:qu];
    }

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    
    if( [elementName isEqualToString:@"province"]) {
        [_provinces setObject:[_cities copy] forKey:_currentProvince];
        _cities = nil;
        _currentProvince = nil;
    }
    if( [elementName isEqualToString:@"city"]) {
         [_cities setObject:[self.dicts copy] forKey:_currentCity];
         _dicts= nil;
         _currentCity = nil;
    }
    if( [elementName isEqualToString:@"district"]) {
       
        
    }

}


    
-(void)parserDidEndDocument:(NSXMLParser *)parser{
  
    
}




@end
